//
//  KCCalloutAnnotationView.m
//  TheElevator
//
//  Created by lwx on 17/3/20.
//  Copyright © 2017年 吕金状. All rights reserved.
//

#import "KCCalloutAnnotationView.h"

@interface KCCalloutAnnotationView()
{
    NSString *phone;
}

@property (nonatomic,strong)UIView *backgroundView;
@property (nonatomic,strong)UILabel *nameLabel;
@property (nonatomic,strong)UILabel *phoneLabel;
@property (nonatomic,strong)UILabel *locationLabel;

@end

@implementation KCCalloutAnnotationView

- (instancetype)init
{
    if(self=[super init])
    {
        [self setUI];
        self.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tableHeadViewCkick)];
        [self addGestureRecognizer:tap];
    }
    return self;
}
- (instancetype)initWithFrame:(CGRect)frame
{
    if (self=[super initWithFrame:frame])
    {
        [self setUI];
    }
    return self;
}

-(void)setUI
{
    self.layer.anchorPoint = CGPointMake(0.5, 1);
    
    _backgroundView = [[UIView alloc]init];
    _backgroundView.backgroundColor=[UIColor whiteColor];
    [self addSubview:_backgroundView];
    
    _nameLabel = [[UILabel alloc]init];
    _nameLabel.lineBreakMode=NSLineBreakByWordWrapping;
    _nameLabel.font = DEF_FontSize_12;
    _nameLabel.numberOfLines = 0;
    [_backgroundView addSubview:_nameLabel];
    
    _phoneLabel = [[UILabel alloc]init];
    _phoneLabel.lineBreakMode=NSLineBreakByWordWrapping;
    _phoneLabel.font = DEF_FontSize_12;
    [_backgroundView addSubview:_phoneLabel];
    
    _locationLabel = [[UILabel alloc]init];
    _locationLabel.lineBreakMode=NSLineBreakByWordWrapping;
    _locationLabel.font = DEF_FontSize_12;
    _locationLabel.numberOfLines = 0;
    [_backgroundView addSubview:_locationLabel];
}

+ (instancetype)calloutViewWithMapView:(MKMapView *)mapView
{
    KCCalloutAnnotationView *calloutView = (KCCalloutAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:NSStringFromClass([KCCalloutAnnotationView class])];
    if (!calloutView)
    {
        calloutView = [[KCCalloutAnnotationView alloc]init];
    }
    return calloutView;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    self.bounds = CGRectMake(0, 0, 100, 45);
    _backgroundView.frame = CGRectMake(0, 0, self.width, self.height - 15);
    _nameLabel.frame = CGRectMake(3, 0, self.width-6,  30);
}

- (void)setAnnotation:(id<MKAnnotation>)annotation
{
    [super setAnnotation:annotation];
    KCCalloutAnnotation *annotation1 = (KCCalloutAnnotation *)annotation;
    
    [self getCoordinate:annotation1.coordinate];
}

- (void)getCoordinate:(CLLocationCoordinate2D )coordinate{
     CLLocation *cl = [[CLLocation alloc] initWithLatitude:coordinate.latitude longitude:coordinate.longitude];
    CLGeocoder *geoCoder = [[CLGeocoder alloc]init];
    [geoCoder reverseGeocodeLocation:cl completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
        for (CLPlacemark *place in placemarks) {
            //            NSDictionary *location = [place addressDictionary];
            //                        NSLog(@"国家：%@",[location objectForKey:@"Country"]);
            //                        NSLog(@"城市：%@",[location objectForKey:@"State"]);
            //                        NSLog(@"区：%@",[location objectForKey:@"SubLocality"]);
            NSLog(@"位置：%@",place.name);
            NSLog(@"国家：%@",place.country);
            NSLog(@"城市：%@",place.locality);
            NSLog(@"区：%@",place.subLocality);
            NSLog(@"街道：%@",place.thoroughfare);
            NSLog(@"子街道：%@",place.subThoroughfare);
            self.nameLabel.text = [NSString stringWithFormat:@"%@%@%@",place.locality,place.subLocality,place.name];
        }
    }];

}

- (void)tableHeadViewCkick{
    if (_getCalloutAnnotationClick) {
        _getCalloutAnnotationClick(self.nameLabel.text);
    }
}


@end
